package com.chain.BrotherExchange.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.event.UserAssetsChangeEvent;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.ViewUtils;
import com.chain.BrotherExchange.viewmodel.AssetTransferViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;

/**
 * 资产划转
 */
public class AssetTransferActivity extends BaseActivity {

    @Bind(R.id.to_text) TextView mToTextView;
    @Bind(R.id.from_text) TextView mFromTextView;
    @Bind(R.id.all_text_view) View mAllTextView;
    @Bind(R.id.affirm_button) View mAffirmButton;
    @Bind(R.id.coin_type_text) TextView mCoinTypeText;
    @Bind(R.id.transfer_edit) EditText mTransferEditText;
    @Bind(R.id.balance_text_view) TextView mBalanceTextView;

    private String mCoinId;
    private AssetTransferViewModel mViewModel;

    public static void launch(TextView balanceView, TextView typeView, String coinId, int type) {
        Activity activity = (Activity) balanceView.getContext();
        Pair balancePair = new Pair<>(balanceView, ViewCompat.getTransitionName(balanceView));
        Pair typePair = new Pair<>(typeView, ViewCompat.getTransitionName(typeView));
        ActivityCompat.startActivity(activity,
                new Intent(activity, AssetTransferActivity.class)
                        .putExtra(C.EXTRA_BALANCE, balanceView.getText().toString())
                        .putExtra(C.EXTRA_COIN_TYPE, typeView.getText().toString())
                        .putExtra(C.EXTRA_COIN_ID, coinId)
                        .putExtra(C.EXTRA_PAGE_TYPE, type),
                ActivityOptionsCompat.makeSceneTransitionAnimation(activity, balancePair, typePair).toBundle());
    }


    @Override
    protected int getLayoutID() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return R.layout.activity_asset_transfer;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_asset_transfer);

        // 视图模型
        mViewModel = ViewModelProviders.of(this).get(AssetTransferViewModel.class);

        // 页面类型
        int pageType = getIntent().getIntExtra(C.EXTRA_PAGE_TYPE, -1);
        if (pageType == C.VALUE_COIN_TYPE){
            mToTextView.setText(R.string.coin_coin);
            mFromTextView.setText(R.string.legal_tender);
            mAffirmButton.setOnClickListener(v -> transferToCurrency());
        } else if (pageType == C.VALUE_LEGAL_CURRENCY_TYPE){
            mToTextView.setText(R.string.legal_tender);
            mFromTextView.setText(R.string.coin_coin);
            mAffirmButton.setOnClickListener(v -> transferToLegalTender());
        }

        // 货币类型及余额
        mCoinId = getIntent().getStringExtra(C.EXTRA_COIN_ID);
        mBalanceTextView.setText(getIntent().getStringExtra(C.EXTRA_BALANCE));
        mCoinTypeText.setText(getIntent().getStringExtra(C.EXTRA_COIN_TYPE));

        // 所有余额
        mAllTextView.setOnClickListener(v ->
                ViewUtils.setTextAndSelectionEnd(mBalanceTextView, mTransferEditText)
        );
    }

    private void transferToCurrency() {
        String number = mTransferEditText.getText().toString();
        mViewModel.transferToCurrency(mCoinId, number).observe(this, resource -> {
            switch (resource.state) {
                case ERROR:
                    Androids.makeText(resource.message);
                    break;

                case SUCCESS:
                    Androids.makeText(resource.source.getMsg());
                    EventBus.getDefault().post(new UserAssetsChangeEvent());
                    finish();
                    break;
            }
        });
    }

    private void transferToLegalTender() {
        String number = mTransferEditText.getText().toString();
        mViewModel.transferToLegalTender(mCoinId, number).observe(this, resource -> {
            switch (resource.state) {
                case ERROR:
                    Androids.makeText(resource.message);
                    break;

                case SUCCESS:
                    Androids.makeText(resource.source.getMsg());
                    EventBus.getDefault().post(new UserAssetsChangeEvent());
                    finish();
                    break;
            }
        });
    }
}
